package com.sdtele.tta.operation;


import java.util.List;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.User;
import com.sdtele.tta.models.VoteOperation;
import com.sdtele.tta.models.VoteTask;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationChgVoteImplement extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		long voteId = jsonRequest.getJsonObject().get("voteId").getAsLong();
		long action= jsonRequest.getJsonObject().get("action").getAsLong();
		long userId= jsonRequest.getJsonObject().get("userId").getAsLong();
		User curUser = (User) request.getSession().getAttribute("user");
		User voteOp=Dac.getInstance().getVoteOperationbyOwner(voteId);

		if (curUser == null) {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
			return jres;
		}
          if(action==1) { 
        	  if(voteOp.getId()==curUser.getId()){
        	 VoteOperation voteOperation=new VoteOperation();
              	 voteOperation.setVoteId(voteId); 
              	 voteOperation.setUserId(userId);
              	 voteOperation.setOperation(1);
              	  Dac.getInstance().addVoteOperation(voteOperation);
              	  User opUser=Dac.getInstance().getUser(voteOperation.getUserId());
              	  if(voteOperation.getId()>0) {
              		jres.setStatus(0);
      				jres.setMessage("添加投票人成功");
      				jres.setEntity(opUser);
        	  }else {
        			jres.setStatus(1);
      				jres.setMessage("添加投票人失败");
        	  }
       }else {
    	   jres.setStatus(1);
		  jres.setMessage("用户非投票任务创建者，添加投票人失败");
       }
    }
        	  if(action==0) {
            	if(voteOp.getId()==curUser.getId()){
            		  if( Dac.getInstance().delVoteoperation(voteId, userId, 1)) {
                    		jres.setStatus(0);
            				jres.setMessage("删除投票人成功");
                    	}else {
            			jres.setStatus(1);
          				jres.setMessage("删除投票人失败");
            	  }
           }else {
        	   jres.setStatus(1);
    		  jres.setMessage("用户非投票任务创建者，添加投票人失败");
           }
        }
        	 
        	  int  voteSize=0;
        	  
        	 List<VoteOperation> opSize= Dac.getInstance().getVoteTaskSize(voteId);
        	  if(opSize!=null) {
                voteSize=opSize.size();
        	  }else {
        		   voteSize=0;
        	  }
        	  int  voteSloveSize=0;
        	  List<VoteOperation> voteSizeofSloved= Dac.getInstance().getVoteTaskSizebysloved(voteId);
        	  if(voteSizeofSloved!=null) {
        		  voteSloveSize=voteSizeofSloved.size();
        	  }else {
        		  voteSloveSize=0;
        	  }
           
          String vote=voteSloveSize+"/"+voteSize;
          VoteTask  votetask= Dac.getInstance().getVoteTaskbyVoteid(voteId);
          System.out.println(votetask);
          votetask.setCompletion(vote);
          if(Dac.getInstance().chgVoteTask(votetask)) {
        	   System.out.println(votetask);
        		jres.setStatus(0);
      			jres.setMessage("投票任务完成更新");
          }else {
        	    jres.setStatus(1);
    			jres.setMessage("投票任务更新失败");
          }
         
        return  jres ;
}
}